package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.SensitiveMapper;
import com.heima.wemedia.service.SensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, WmSensitive> implements SensitiveService {



    @Override
    public ResponseResult insert(WmSensitive dto) {
        //1.校验
        if(dto==null || StringUtils.isBlank(dto.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.校验名称是否存在
//        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(WmSensitive::getSensitives, dto.getSensitives());
        //或者
        WmSensitive db = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, dto.getSensitives()));
        if(db!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "该敏感词已经存在");
        }

        //3.保存
        dto.setCreatedTime(new Date());
        save(dto);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(WmSensitive dto) {
        //1.校验
        if(dto==null || StringUtils.isBlank(dto.getSensitives()) || dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive wmSensitive = getById(dto.getId());
        if(wmSensitive==null){
            //说明id有问题
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.已存在的敏感词不能保存
        WmSensitive db = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, dto.getSensitives()));
        if(db!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "该敏感词已经存在");
        }

        //3.修改
        updateById(dto);

        //4.返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult del(Integer id) {
        //1.校验
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive wmSensitive = getById(id);
        if(wmSensitive==null){
            //说明id有问题
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //2.删除
        removeById(id);

        //3.返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult list(SensitiveDto dto) {
        //1.校验
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();


        //2.分页条件查询 mybatisplus
        IPage pageParam = new Page(dto.getPage(), dto.getSize());//mybatisplus分页条件对象
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like(WmSensitive::getSensitives, dto.getName());
        }
        //或者下面写法
        //wrapper.like(StringUtils.isNotBlank(dto.getName()), WmSensitive::getSensitives, dto.getName());
        IPage page = page(pageParam, wrapper);


        //3.封装结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int)page.getTotal());
        responseResult.setData(page.getRecords());

        //下面这种写法不对
        //return ResponseResult.okResult(responseResult);

        //应该是
        return responseResult;
    }
}
